package com.qianli.user.facade.query.impl.auth;

import com.fqgj.common.api.Response;
import com.qianli.user.application.UserAuthApplication;
import com.qianli.user.enums.CreditAuthTypeEnum;
import com.qianli.user.facade.query.auth.UserAuthPictureQueryServiceFacade;
import com.qianli.user.facade.query.auth.UserAuthQueryServiceFacade;
import com.qianli.user.ro.UserStateCode;
import com.qianli.user.ro.auth.UserAuthFaceRecognitionRO;
import com.qianli.user.ro.auth.UserAuthInfoRO;
import com.qianli.user.ro.query.UserAuthFaceQueryRO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * name:人脸认证查询
 * auth:ting.li@qianli-inc.com
 * date:2018/1/9 10:43
 * version: v1.0.0
 **/
@Service("userAuthPictureQueryServiceFacade")
public class UserAuthPictureQueryServiceFacadeImpl implements UserAuthPictureQueryServiceFacade{

    private static final Logger LOGGER = LoggerFactory.getLogger(UserAuthQueryServiceFacade.class);

    @Autowired
    private UserAuthApplication userAuthApplication;

    @Override
    public Response<UserAuthFaceRecognitionRO> getUserAuthFaceRecognitionByType(UserAuthFaceQueryRO query) {
        if(null == query){
            LOGGER.info("UserAuthQueryServiceFacade.getUserAuthFaceRecognitionByType param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        if(null == query.getAppCode()||null == query.getBiz()){
            LOGGER.info("UserAuthQueryServiceFacade.getUserAuthFaceRecognitionByType param appCode and biz can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK,"appCode and biz 不能为空");
        }
        if(null == query.getUserCode()){
            LOGGER.info("UserAuthQueryServiceFacade.getUserAuthFaceRecognitionByType param userCode can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK,"userCode不能为空");
        }
        if(null == query.getType()){
            LOGGER.info("UserAuthQueryServiceFacade.getUserAuthFaceRecognitionByType param authType can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK,"Type不能为空");
        }
        Response<UserAuthInfoRO> response = userAuthApplication.load(query.getUserCode(),
                CreditAuthTypeEnum.FACE.getType());
        if(!response.isSuccess()){
            return Response.error(UserStateCode.getEnumByType(response.getCode()));
        }
        UserAuthFaceRecognitionRO faceRecognitionRO = null;
        UserAuthInfoRO authInfoRO = response.getData();
        if(null != authInfoRO && CollectionUtils.isNotEmpty(authInfoRO.getAuthFaceRecognitions())){
            for(UserAuthFaceRecognitionRO face:authInfoRO.getAuthFaceRecognitions()){
                if(face.getType().equals(query.getType())){
                    faceRecognitionRO = face;
                }
            }
        }
        return Response.ok(faceRecognitionRO);
    }
}
